﻿using System;
using System.Data;
using System.Collections.Generic;

using System.Text;
using Portal.Environment;
using Portal.Language;
using Portal.Interface;
//using Portal.LibraryService;


namespace Portal.CDSP
{
    public class RSimpleSyndication : WebPart
    {
        public override string WebPartId
        {
            get
            {
                return "WebSite.RSS";
            }
        }

        public override string WebPartFolder
        {
            get
            {
                return "Portal.CDSP";
            }
        }
        public override string Title
        {
            get
            {
                return GetText("FTitle", "CDSP-RSS");
            }
        }

        public override ViewCls[] Views
        {
            get
            {
                return new ViewCls[]{
                    new ViewCls("DefaultView",GetText("DefaultView","Really Simple Syndication"),1)
                };
            }
        }


        public override string NameSpace
        {
            get
            {
                return "Portal.CDSP";
            }
        }

        public override string ClassName
        {
            get
            {
                return "RSimpleSyndication";
            }
        }


        [AjaxPro.AjaxMethod]
        public override string ClientRenderWebPart(string BelongSite,
            string TabModuleId, string ViewName,
            string HasPorlet, LoadParamCls loadParamCls)
        {
            if (ViewName.Equals("DefaultView"))
            {
                return ClientRenderWebPartDefault(TabModuleId, HasPorlet);
            }
            return GetText("ViewNotFound", "Không tìm thấy View [" + ViewName + "]");
        }


        public override DataTable ReadingDataService(string belongSite, string ActiveLanguage, ref string IdField, ref string SubjectField)
        {
            IdField = "Id";
            SubjectField = "Subject";
            DataTable dtResult = new DataTable();
            dtResult.Columns.Add("Id");
            dtResult.Columns.Add("Subject");
            dtResult.Rows.Add(new object[]{
                "4062D500-1CA2-41A3-B055-04C0C28FD63A","Các kênh RSS do Website cung cấp"
            });
            return dtResult;
        }

        private string ClientRenderWebPartDefault(string TabModuleId, string HasPorlet)
        {
            try
            {
                TabModuleCls TabModuleCls = new TabModuleBLL().CreateModelFromXml(TabModuleId);
                if (TabModuleCls == null) throw new Exception("[" + TabModuleId + "] " + GetText("TabModuleIdNull", "not found!"));
                string belongSite = TabModuleCls.belongSite;
                string RssUrl = string.Empty;
                string RssName = string.Empty;
                string RssSaveFile = string.Empty;
                string ChanelId = string.Empty;
                string ChanelTitle = string.Empty;
                string Ext = "xml";
                RssChanelCls[] RssChanels = RSimpleSyndicationBLL.GetRssChannel(Ext, belongSite);
                StringBuilder Html = new StringBuilder();
                    Html.Append(
                        WebPartUtilities.AttachJavascript(WebPartFolder, "rssfeed.js")+                
                        "<div id=\"leftsub\">" +
                                "<div class=\"leftsubHeader\">" +
                                 "<h3>Các kênh Rss do website cung cấp.</h3>" +
                                   "<div class=\"clrfix\"></div>" +
                                 "</div>"+
                        "<div class=\"leftsubContent\">" +
                        "<div class=\"leftsubContentwrappin2\">" +
                        "<table class=\"Rss\">");
                for (int iIndex = 0; iIndex < RssChanels.Length; iIndex++)
                {
                    RssUrl = RssChanels[iIndex].ChanelUrl.ToString();
                    RssName = RssChanels[iIndex].ChanelTitle.ToString();
                    RssSaveFile = RssChanels[iIndex].ChanelSaveFile.ToString();
                    ChanelId = RssChanels[iIndex].ChanelId.ToString();
                    ChanelTitle = RssChanels[iIndex].ChanelTitle.ToString();
                    Html.Append(
                    "<tr>" +
                        "<td class = \"RssImg\">&nbsp;&nbsp;<a href=\"javascript:ClientSideCallRss('" + ChanelId + "','" + belongSite + "','" + ChanelTitle + "');\" ><img src=images/rss.gif></a>  </td>" +
                        "<td class = \"RssName\">&nbsp;&nbsp;<a href=\"javascript:ClientSideCallRss('" + ChanelId + "','" + belongSite + "','" + ChanelTitle + "');\"><b>" + RssName + "</b></a> </td>" +
                    "</tr>");
                }
                Html.Append(
                    "</table>" +        
                    "</div>"+
                    "</div>" +
                    "<div class=clrfix></div>" +
                    "</div>");
                return Html.ToString();
            }
            catch (Exception ex)
            {
                return "ERROR:" + ex.ToString();
            }
        }

        public override bool AllowFrontService
        {
            get
            {
                return true;
            }
        }

        public override void RegAjaxPro(System.Web.UI.Page ActivePage)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(RSimpleSyndication), ActivePage);

        }

        [AjaxPro.AjaxMethod()]
        public string ServerSideCallRssChannel(string GroupNewsId, string belongSite, string ChanelTitle)
        {
            try
            {
                //var rss = new RssChannelBll();
                //var RSSChannel = rss.MakeXmlDataForChanel("RSS", "RSS - SODC Website", "sodc.com.vn", "CMC", "dvtien", DateTime.Now, DateTime.Now, GroupNewsId, "xml");                
                var RSSChannel = new RSimpleSyndicationBLL().MakeXmlDataForChanel(ChanelTitle, "RSS-CDSP Website", "http://cdspna.edu.vn", "CMC", "dvtien", DateTime.Now, DateTime.Now, GroupNewsId, "xml", belongSite);
                return RSSChannel.Url;

            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message;
            }
        }

    }
}
